//
//  LoginController.swift
//  Order
//
//  Created by Lazyloading on 2020/7/2.
//  Copyright © 2020 lazyloading. All rights reserved.
//

import UIKit
import QMUIKit
import LEEAlert
import PKHUD


/// 登录
class LoginController: UIViewController {

    @IBOutlet weak var loginBtn: UIButton!
    @IBOutlet weak var passwordField: QMUITextField!
    @IBOutlet weak var accountField: QMUITextField!
    @IBOutlet weak var welcomeLabel: UILabel!
    @IBOutlet weak var logoImg: UIImageView!
    
    @IBOutlet weak var topConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.loadDefaultSetting();
        self.initSubViews();
    }
    
    // MARK: 👉🏻 🐷Life cycle🐷
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        
        self.topConstraint.constant = ISIPHONEX() ?  130 : 90;
        
    }
    // MARK: 👉🏻 🐷System Delegate🐷
    // MARK: 👉🏻 🐷Custom Delegate🐷
    // MARK: 👉🏻 🐷Event  Response🐷
    @IBAction func loginAction(_ sender: Any) {
        
        let param = [
            "username" : self.accountField.text ?? "",
            "password" : self.passwordField.text?.md5() ?? ""]
            as [String : Any];
        
        
        MoyaRequest(.login(param), success: { (response : BaseResponse<UserinfoModel>) in
            if response.respCode == 200{
                ZMLog(response.data?.save());
                
                HUD.flash(.label("登录成功"), delay: 1);
                UIApplication.shared.delegate?.window??.rootViewController = AppDelegate.transformMain();
            }else{
                HUD.flash(.label("登录失败"), delay: 1);
    
            }
        }) { (error) in
            ZMLog(error);
        }
    }
    
    @IBAction func registerAction(_ sender: Any) {
        self.navigationController?.pushViewController(RegisterController(), animated: true);
        
    }
    // MARK: 👉🏻 🐷Private Methods🐷
    //修改状态
    func changeState()  {
        
    }
    
    //校验参数
    func checkParam() -> Bool{
        if self.accountField.text != nil && self.accountField.text?.count ?? 0 > 0 {
            return false;
        }
        return false;
    }

    @objc func textfieldValueChanged(textfield : UITextField) {
        ZMLog(textfield.text);
        
        if self.accountField.text!.count  > 0 && self.passwordField.text!.count > 0 {
            self.loginBtn.isUserInteractionEnabled = true;
            self.loginBtn.backgroundColor = ZMColor.Orange.orange1;
        }else{
            self.loginBtn.isUserInteractionEnabled = false;
            self.loginBtn.backgroundColor = ZMHexColor("e4e4e4")
        }
        
    }
    // MARK: 👉🏻 🐷Lazy loading🐷
    // MARK: 👉🏻 🐷Init SubViews🐷
    func loadDefaultSetting()  {
        self.loginBtn.isUserInteractionEnabled = false;
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default);
        self.navigationController?.navigationBar.shadowImage = UIImage();
    }
    
    func initSubViews()  {
        
        self.accountField.qmui_borderColor = ZMHexColor("e4e4e4");
        self.passwordField.qmui_borderColor = ZMHexColor("e4e4e4");
        self.accountField.qmui_borderPosition = .bottom;
        self.passwordField.qmui_borderPosition = .bottom;
        
        self.accountField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
        self.passwordField.addTarget(self, action: #selector(textfieldValueChanged), for: .editingChanged);
    }

}
